<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.binary_repr &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Binary operations" href="../routines.bitwise.html" >
    <link rel="next" title="String operations" href="../routines.char.html" >
    <link rel="prev" title="numpy.unpackbits" href="numpy.unpackbits.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.bitwise.html" accesskey="U">Binary operations</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="../routines.char.html" title="String operations"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.unpackbits.html" title="numpy.unpackbits"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.unpackbits.html"
                        title="previous chapter">numpy.unpackbits</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../routines.char.html"
                        title="next chapter">String operations</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-binary-repr">
<h1>numpy.binary_repr<a class="headerlink" href="#numpy-binary-repr" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.binary_repr">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">binary_repr</code><span class="sig-paren">(</span><em class="sig-param">num</em>, <em class="sig-param">width=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/core/numeric.py#L1867-L1979"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.binary_repr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the binary representation of the input number as a string.</p>
<p>For negative numbers, if width is not given, a minus sign is added to the
front. If width is given, the two’s complement of the number is
returned, with respect to that width.</p>
<p>In a two’s-complement system negative numbers are represented by the two’s
complement of the absolute value. This is the most common method of
representing signed integers on computers <a class="reference internal" href="#r962252997619-1" id="id1">[1]</a>. A N-bit two’s-complement
system can represent every integer in the range
<img class="math" src="../../_images/math/83aa226b503298ac627b9c8da57e0cf3871420ae.svg" alt="-2^{N-1}"/> to <img class="math" src="../../_images/math/451a8c2e6e6a58c4bd7b3b4e7ed745edf7c9e62a.svg" alt="+2^{N-1}-1"/>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>num</strong><span class="classifier">int</span></dt><dd><p>Only an integer decimal number can be used.</p>
</dd>
<dt><strong>width</strong><span class="classifier">int, optional</span></dt><dd><p>The length of the returned string if <em class="xref py py-obj">num</em> is positive, or the length
of the two’s complement if <em class="xref py py-obj">num</em> is negative, provided that <em class="xref py py-obj">width</em> is
at least a sufficient number of bits for <em class="xref py py-obj">num</em> to be represented in the
designated form.</p>
<p>If the <em class="xref py py-obj">width</em> value is insufficient, it will be ignored, and <em class="xref py py-obj">num</em> will
be returned in binary (<em class="xref py py-obj">num</em> &gt; 0) or two’s complement (<em class="xref py py-obj">num</em> &lt; 0) form
with its width equal to the minimum number of bits needed to represent
the number in the designated form. This behavior is deprecated and will
later raise an error.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 1.12.0.</span></p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>bin</strong><span class="classifier">str</span></dt><dd><p>Binary representation of <em class="xref py py-obj">num</em> or two’s complement of <em class="xref py py-obj">num</em>.</p>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="numpy.base_repr.html#numpy.base_repr" title="numpy.base_repr"><code class="xref py py-obj docutils literal notranslate"><span class="pre">base_repr</span></code></a></dt><dd><p>Return a string representation of a number in the given base system.</p>
</dd>
<dt><a class="reference external" href="https://docs.python.org/dev/library/functions.html#bin" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">bin</span></code></a></dt><dd><p>Python’s built-in binary representation generator of an integer.</p>
</dd>
</dl>
</div>
<p class="rubric">Notes</p>
<p><a class="reference internal" href="#numpy.binary_repr" title="numpy.binary_repr"><code class="xref py py-obj docutils literal notranslate"><span class="pre">binary_repr</span></code></a> is equivalent to using <a class="reference internal" href="numpy.base_repr.html#numpy.base_repr" title="numpy.base_repr"><code class="xref py py-obj docutils literal notranslate"><span class="pre">base_repr</span></code></a> with base 2, but about 25x
faster.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r962252997619-1"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>Wikipedia, “Two’s complement”,
<a class="reference external" href="https://en.wikipedia.org/wiki/Two's_complement">https://en.wikipedia.org/wiki/Two’s_complement</a></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">binary_repr</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">&#39;11&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">binary_repr</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">&#39;-11&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">binary_repr</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="go">&#39;0011&#39;</span>
</pre></div>
</div>
<p>The two’s complement is returned when the input number is negative and
width is specified:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">binary_repr</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="go">&#39;101&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">binary_repr</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="go">&#39;11101&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>